#include "utils.h"
#include "cell.h"
#include "cell_func.h"
#include "defines.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

extern char filepath[];

const enum Genome DGENOME[GENOME_LENGTH] = {
    G_ROTATE,
    G_MAKE_CELL,
    G_COLLECT_MINERALS,
    G_USE_MINERALS,
    G_MINERALS_ENERGY,
    G_MINERALS_ENERGY,
    G_MINERALS_ENERGY,
    G_MINERALS_ENERGY,
    G_MINERALS_ENERGY,
    G_CHECK_GENOME,
    G_JUMP_GENOME, 1,
    //G_INFECT,
};

void init(struct World *w, int width, int height) {
    w->width  = width;
    w->height = height;
    w->time = w->dayLength/2;
    w->timeDirection = true;
    for(int i=0; i<MAX_CELLS; i++)
        if(w->cells[i].type != T_WALL && w->cells[i].type != T_PORTAL)
            w->cells[i] = newNullCell();
    int i = cordsToIndex(w, width/2, 0);
    w->cells[i].color.r = rand() % 255;
    w->cells[i].color.g = rand() % 255;
    w->cells[i].color.b = rand() % 255;
    w->cells[i].energy = 20;
    w->cells[i].rotation = rand() % 8;
    for(int j=0; j < GENOME_LENGTH; j++) {
        w->cells[i].genome[j] = DGENOME[j];
    }
    w->cells[i].type = T_CELL;
}

void toggleBool(bool *b) {
    (*b) = 1 - (*b);
}

void importWorld(char file[], struct World *w) {
    FILE *f = fopen(file, "rb");
    if(f != NULL) {
        (void)!fread(w, sizeof(*w), 1, f);
        strcpy(filepath, file);
        if(w->mutationChance == 0) w->mutationChance = 500;
    } else {
        fprintf(stderr, "It's inposible to load %s\n", file);
        exit(-1);
    }
}

void exportWorld(struct World *w) {
    FILE *f = fopen(filepath, "wb");
    if(f == NULL)
        puts("Failed to export to file!");
    fwrite(w, sizeof(struct World), 1, f);
    fclose(f);
}

void printInfoAboutCell(struct World *w, int x, int y) {
    int cellIndex = cordsToIndex(w, x, y);
    struct Cell* cell = &w->cells[cellIndex];
    printf("------------\n");
    printf("Type: ");
    switch(cell->type) {
    case(T_CELL):
        printf("cell, ");
        break;
    case(T_WALL):
        printf("wall, ");
        break;
    case(T_FOOD):
        printf("food, ");
        break;
    case(T_NOTHING):
        printf("nothing, ");
        break;
    case(T_MINWALL):
        printf("mineral wall, ");
        break;
    case(T_PORTAL):
        printf("portal, ");
        break;
    }
    printf("Index: %d, Cords: (%d, %d), RGB: #%02x%02x%02x, Age: %d, Energy: %d, Minerals: %d, Rotation:",
            cellIndex, x, y, cell->color.r, cell->color.g, cell->color.b, cell->age, cell->energy, cell->minerals);
    switch(cell->rotation) {
    case(D_LDOWN):
        printf("↙️ ");
        break;
    case(D_DOWN):
        printf("⬇️ ");
        break;
    case(D_RDOWN):
        printf("↘️ ");
        break;
    case(D_LEFT):
        printf("⬅️ ");
        break;
    case(D_RIGHT):
        printf("➡️ ");
        break;
    case(D_LUP):
        printf("↖️ ");
        break;
    case(D_UP):
        printf("⬆️ ");
        break;
    case(D_RUP):
        printf("↗️ ");
        break;
    }
    printf("\nGenome:");
    for(int genomeIndex=0; genomeIndex<GENOME_LENGTH; genomeIndex++) {
        printf(" %02d", cell->genome[genomeIndex]);
    }
    printf("\n");
}
